#!/bin/bash
Usage(){
	echo "Usage: $0 <phone list file name> <count of phone to process at one time>"
	exit 1
}
if [ -z $1 ]
then
	Usage
else
	FILENAME="$1"
fi
if [ -z $2 ]
then
        Usage
else
        INTERVAL="$2"
fi
TMPFILE="tmp.list"
MSG="您预约的《2019财经头条经济学家年会》现场直播即将开始！请前往财经头条APP观看，https://url.cn/5moddUd"
SEND_LIST=""
COUNT=$(cat $FILENAME | wc -l)
echo "There are $COUNT to process..."
cp $FILENAME $TMPFILE
while true
do
	#echo "sed -n \"1,${INTERVAL}p\" $TMPFILE"
	CURRENT_LIST=$(sed -n "1,${INTERVAL}p" $TMPFILE)
	if [ -z "$CURRENT_LIST" ]
	then
		break
	else
		echo "merge send_list ..."
		echo $CURRENT_LIST
		for phone in $CURRENT_LIST
		do
			SEND_LIST=${phone},${SEND_LIST}
		done
		echo $SEND_LIST
		echo "merged adb command"
		echo "adb shell am start -a android.intent.action.SENDTO -d sms:$SEND_LIST --es sms_body $MSG"
		echo "excute adb command ..."
		adb shell am start -a android.intent.action.SENDTO -d sms:$SEND_LIST --es sms_body $MSG
		sleep 2
		#模拟发送
		adb shell input keyevent 22
		sleep 2
		adb shell input keyevent 66
		sleep 10
		adb shell input keyevent 3
		sleep 5
		echo "delete processed phone list"
		sed -i "1,${INTERVAL}d" $TMPFILE
		echo "reset send_list"
		SEND_LIST=""
	fi	
done

#adb shell am start -a android.intent.action.SENDTO -d sms:$SEND_LIST --es sms_body $MSG

